package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.SysUser;
import com.hmall.service.AuctionService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.AuctionRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.math.BigDecimal;

/**
 * @ClassName AuctionController
 * @Description
 * @Author zhoutao
 * @Date 2018/12/26 10:27
 * @Version 2.0
 **/
@Api(value = "竞拍接口", description = "竞拍接口说明")
@RequestMapping("/auction/")
@RestController
public class AuctionController {

    TokenUtil tokenUtil = new TokenUtil();

    private final JedisPool jedisPool;

    private final AuctionService auctionService;

    @Autowired
    public AuctionController(JedisPool jedisPool, AuctionService auctionService) {
        this.jedisPool = jedisPool;
        this.auctionService = auctionService;
    }

    /**
     * @return com.hmall.common.ServerResponse<com.hmall.vo.AuctionVo>
     * @Date 2018/12/26 11:10
     * @Param [token, type]
     * @Description 竞拍列表，预热中0，进行中1，已结束2
     */
    @LoginRequired
    @GetMapping("listAuction")
    @ApiOperation(value = "列出竞拍列表", notes = "input：type 0：预热中 1：进行中 2：已结束|output:对于结束的竞拍需要列出原因 2表示成功 3表示流拍,applyStatus:-1还没申请竞拍资格，0资格申请中 1竞拍资格申请失败 但是可以重新申请（必须在预热状态）2竞拍资格申请成功，")
    public ServerResponse listAuction(String token, Integer type, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return auctionService.listAuction(user.getUserId(), type, pageNum, pageSize);

    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/26 13:30
     * @Param [token, price, packageNo]
     * @Description //参与竞拍
     */
    @LoginRequired
    @PostMapping("postAuction")
    @ApiOperation(value = "参与竞拍", notes = "发起竞拍动作")
    public ServerResponse postAuction(String token, BigDecimal price, Long packageNo) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return auctionService.postAuction(user.getUserId(), price, packageNo, user);
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/27 11:19
     * @Param [token, packageNo]
     * @Description //竞拍的产品详情
     */
    @GetMapping("listAuctionProduct")
    @ApiOperation(value = "得到竞拍的产品详情")
    public ServerResponse listAuctionProduct(String token, Long packageNo) {

        return auctionService.listAuctionProduct(packageNo);
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/26 14:40
     * @Param []
     * @Description 定时任务修改竞拍状态
     */
    @GetMapping("updateAuctionStatus")
    @ApiOperation(value = "更新竞拍状态", notes = "为定时任务服务, 1表示从预热变成竞价 2表示从竞价变成结束")
    public ServerResponse updateAuctionStatus(Integer type) {


        auctionService.updateAuctionStatus(1);
        return auctionService.updateAuctionStatus(2);

    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/26 15:32
     * @Param [token, type]
     * @Description //个人竞拍列表  竞拍中，竞拍成功，竞拍失败，竞拍报名
     */
    @LoginRequired
    @GetMapping("listSelfAuction")
    @ApiOperation(value = "列出个人竞拍列表", notes = "1代表竞拍中 2代表竞拍成功 3代表竞拍失败 4竞拍报名 5代表竞拍成功和竞拍失败 output:(5个人竞拍历史状态)status 1成功 2失败 (4竞拍报名)applyStatus 0竞拍申请中 1竞拍申请失败 2竞拍申请成功")
    public ServerResponse listSelfAuction(String token, Integer type, @RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return auctionService.listSelfAuction(user.getUserId(), type, pageNum, pageSize);
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/27 10:10
     * @Param [token, phone, packageNo]
     * @Description 个人竞拍申请
     */
    @LoginRequired
    @PostMapping(value = "applyAuction")
    @ApiOperation(value = "个人竞拍申请")
    public ServerResponse applyAuction(String token, String phone, Long packageNo) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return auctionService.applyAuction(user.getUserId(), phone, packageNo);
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/27 16:38
     * @Param [type]
     * @Description //TODO 得到竞拍规则文案
     */
//    @LoginRequired
    @GetMapping(value = "getAuctionRule")
    @ApiOperation(value = "得到竞拍规则", notes = "1:代表竞拍中订单规则,2:代表竞拍预热订单规则，3:代表PC竞拍规则（只有一条）")
    public ServerResponse<AuctionRuleVo> getAuctionRule(String token, Integer type, Long packageNo) throws Exception {

//        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return auctionService.getAuctionRule(type, packageNo);
    }

    @PostMapping("auctionRabbitTest")
    @ApiOperation(value = "rabbitmq 竞拍测试", notes = "device传递IOS-,ANDROID-, PC-")
    public ServerResponse test(@RequestParam Long packageNo, Integer userId, String device) {

        return auctionService.test(packageNo, userId, device);
    }
}
